package cn.com.landsky.camera.common.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/***
 ** @category ...
 ** @author qing.yunhui
 ** @email: qingyunhui@landsky.cn
 ** @createTime: 2019/1/3-15:17
 **/
public class DateUtil {

    /***（IOS8601格式：yyyy-MM-dd’T’HH:mm:ss.SSSXXX）例如北京时间：2017-06-14T00:00:00.000+08:00 **/
    public static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    public static final String YYYY_MM_DD_HH_MM_SS_MS = "yyyy-MM-dd HH:mm:ss:ms";
    public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";


    /**
     * 将指定日期转换成指定格式的字符串
     * @param data 待转换的日期(可以是，字符串格式的日期，也可以是 date类型的日期)
     * @param formatString 待转换时间的格式
     * @return
     */
    public static String dateToString(Object data, String formatString) {
        String dateString = "";
        if(StringUtil.isEmptys(data,formatString)){return dateString;}
        SimpleDateFormat formatter = new SimpleDateFormat(formatString);
        dateString = formatter.format(data);
        return dateString;
    }

    /**
     * 获取当前时间
     * @param formatString
     * @return String
     */
    public static String getStringDate(String formatString) {
        if(StringUtil.isEmptys(formatString)){return null;}
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat(formatString);
       return outFormat.format(now);
    }

    /**
     * 将长时间格式字符串转换为时间
     * @param strDate
     * @param formatString
     * @return
     */
    public static Date stringToDate(String strDate, String formatString) {
        if(StringUtil.isEmptys(strDate,formatString)){return null;}
        SimpleDateFormat formatter = new SimpleDateFormat(formatString);
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }

    public static void main(String[] args){
        String result=dateToString(new Date(),ISO_8601_FORMAT);
        System.out.println("result1:"+result);
        //"2019-01-10T15:10:00.000+08:00",
        String result2=dateToString("2019-01-10 15:10:11",YYYY_MM_DD_HH_MM_SS);
        System.out.println("result2:"+result2);
    }


}
